package com.bjpowernode.crm.service.impl;

import com.bjpowernode.crm.excepetion.LoginException;
import com.bjpowernode.crm.mapper.UserMapper;
import com.bjpowernode.crm.pojo.Page;
import com.bjpowernode.crm.pojo.User;
import com.bjpowernode.crm.service.UserService;
import com.bjpowernode.crm.utils.MD5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public User findUserByActPwd(String loginAct, String loginPwd,String remoteAddr){
            //根据需求：需要将密码用MD5进行加密，然后再与数据库进行比较【利用工具类完成】
            loginPwd = MD5Util.getMD5(loginPwd);
            User user = userMapper.selectUserActPwd(loginAct, loginPwd);

            //业务逻辑层就需要进行业务需求判断：通过全局异常处理来处理这些业务
            //1. 账号或密码验证错误：在页面上给出提示！
            if(user ==null){
                throw new LoginException("账户名或密码错误");
            }
            //2.判断账户是否失效
            if(user.getExpireTime() !=null && !"".equals(user.getExpireTime())){
                //获取当前时间
                long now = System.currentTimeMillis();

                //获取用户权限的失效时间
                String expireTime = user.getExpireTime();

                //将字符串转为Date日期
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    Date parseTime = dateFormat.parse(expireTime);
                    long userTime = parseTime.getTime();
                    //通过计算与元年的毫秒对比大小，如果用现在的时间毫秒大于户权限的时间毫秒,则有效
                    if(userTime<now){
                        throw new LoginException("登录权限已经失效！");
                    }
                } catch (ParseException e) {
                    throw new LoginException("权限失效！请联系管理员！");
                }
            }

            //3.判断是否锁定：
            if(user.getLockStatus().equals("0")){
                throw new LoginException("账号已被锁定，请联系管理员");
            }

            //判断用户的ip地址是否有访问权限
            String allowIps = user.getAllowIps();
            //allowIps为空表示不作任何限制，多个ip地址用逗号,隔开

            if(!"".equals(allowIps)&& allowIps!=null){
                String[] ips = allowIps.split(",");
                boolean flag =false;
                for (String ip : ips) {
                    if(allowIps.equals(ip)){
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    throw new LoginException("当前ip不允许访问！");
                }
            }


            return user;
    }


    //免密登录
    @Override
    public User selectLoginAuto(String loginAct, String loginPwd, String remoteAddr) {

        User user = userMapper.selectUserActPwd(loginAct, loginPwd);

        if(user!=null){
            //判断ip地址是否有访问权限
            String allowIps = user.getAllowIps();
            if(!"".equals(allowIps)&& allowIps!=null){
                boolean flag =false;
                String[] ips = allowIps.split(",");

                for (String ip : ips) {
                    if(allowIps.equals(ip)){
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                  return null;
                }
            }

        }

     return user;
    }

    @Override
    public List<String> getOption() {
        return userMapper.getOption();
    }

    @Override
    public void getUser(Page page, Map map) {
        //获取并设置总记录数：
       Integer total = userMapper.getTotalUser(map);
       page.setTotalRows(total);
       //根据总记录数：得出总页数
        Integer totalPage = (total-1)/page.getRowsPerPage()+1;
        page.setTotalPages(totalPage);

        //获取并设置： 每页展示的数据
        Integer index =(page.getCurrentPage()-1)*page.getRowsPerPage();
        List<User> data = userMapper.getData(index, page.getRowsPerPage(),map);
        page.setData(data);

    }

    @Override
    public void addUser(User user) {
        user.setLoginPwd(MD5Util.getMD5(user.getLoginPwd()));
        userMapper.addUser(user);
    }

    @Override
    public void delUser(String[] ids) {
        userMapper.delUser(ids);
    }
}
